package o;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ServiceInfo;
import android.os.Bundle;
import android.util.Log;
import com.huawei.agconnect.core.ServiceDiscovery;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class avs {
    private Context ˋ;

    static class b implements Comparator<Entry<String, Integer>>, Serializable {
        private b() {
        }

        public /* synthetic */ int compare(Object obj, Object obj2) {
            return ॱ((Entry) obj, (Entry) obj2);
        }

        public int ॱ(Entry<String, Integer> entry, Entry<String, Integer> entry2) {
            return ((Integer) entry.getValue()).intValue() - ((Integer) entry2.getValue()).intValue();
        }
    }

    avs(Context context) {
        this.ˋ = context;
    }

    public List<avu> ॱ() {
        List<String> ˏ = ˏ();
        List<avu> arrayList = new ArrayList();
        for (String ˏ2 : ˏ) {
            avt ˏ3 = ˏ(ˏ2);
            if (ˏ3 != null) {
                ˏ3.ˏ(this.ˋ);
                Collection ॱ = ˏ3.ॱ(this.ˋ);
                if (ॱ != null) {
                    arrayList.addAll(ॱ);
                }
            }
        }
        return arrayList;
    }

    private <T extends avt> T ˏ(String str) {
        try {
            Class cls = Class.forName(str);
            if (avt.class.isAssignableFrom(cls)) {
                return (avt) Class.forName(str).newInstance();
            }
            Log.e("ServiceRegistrarParser", cls + " must extends from ServiceRegistrar.");
            return null;
        } catch (ClassNotFoundException e) {
            Log.e("ServiceRegistrarParser", "Can not found service class, " + e.getMessage());
            return null;
        } catch (Exception e2) {
            Log.e("ServiceRegistrarParser", "instantiate service class exception " + e2.getLocalizedMessage());
            return null;
        }
    }

    private List<String> ˏ() {
        List<String> arrayList = new ArrayList();
        Bundle ˋ = ˋ();
        if (ˋ == null) {
            return arrayList;
        }
        Map hashMap = new HashMap();
        for (String str : ˋ.keySet()) {
            if ("com.huawei.agconnect.core.ServiceRegistrar".equals(ˋ.getString(str))) {
                String[] split = str.split(":");
                if (split.length == 2) {
                    try {
                        hashMap.put(split[0], Integer.valueOf(split[1]));
                    } catch (NumberFormatException e) {
                        Log.e("ServiceRegistrarParser", "registrar configuration format error:" + e.getMessage());
                    }
                } else if (split.length == 1) {
                    hashMap.put(split[0], Integer.valueOf(1000));
                } else {
                    Log.e("ServiceRegistrarParser", "registrar configuration error, " + str);
                }
            }
        }
        List<Entry> arrayList2 = new ArrayList(hashMap.entrySet());
        Collections.sort(arrayList2, new b());
        for (Entry key : arrayList2) {
            arrayList.add(key.getKey());
        }
        return arrayList;
    }

    private Bundle ˋ() {
        Bundle bundle = null;
        PackageManager packageManager = this.ˋ.getPackageManager();
        if (packageManager != null) {
            try {
                ServiceInfo serviceInfo = packageManager.getServiceInfo(new ComponentName(this.ˋ, ServiceDiscovery.class), DnsConfig.MAX_CACHE_ENTRIES);
                if (serviceInfo == null) {
                    Log.e("ServiceRegistrarParser", "Can not found ServiceDiscovery service.");
                } else {
                    bundle = serviceInfo.metaData;
                }
            } catch (NameNotFoundException e) {
                Log.e("ServiceRegistrarParser", "get ServiceDiscovery exception." + e.getLocalizedMessage());
            }
        }
        return bundle;
    }
}
